package com.lmabbe.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.objects.converter.TenantDatabasesConverter;
import com.lmabbe.tenant.objects.dto.TenantDatabasesDTO;
import com.lmabbe.tenant.objects.entity.TenantDatabases;
import com.lmabbe.tenant.objects.query.TenantDatabasesQuery;
import com.lmabbe.tenant.objects.vo.TenantDatabasesVO;
import com.lmabbe.tenant.service.TenantDatabasesService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户数据库表管理
 *
 * @author lmabbe
 */
@Api(tags = "租户数据库表管理")
@RestController
@RequestMapping("tenantDatabases")
@RequiredArgsConstructor
public class TenantDatabasesController {

    private final TenantDatabasesService tenantDatabasesService;
    private final TenantDatabasesConverter tenantDatabasesConverter;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<TenantDatabasesVO>> list() {
        List<TenantDatabases> list = tenantDatabasesService.list();
        return R.success(tenantDatabasesConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<TenantDatabasesVO>> list(TenantDatabasesQuery query) {
        IPage page = tenantDatabasesService.page(query.createPage(), query.createQuery());
        page.setRecords(tenantDatabasesConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<TenantDatabasesVO> getById(@PathVariable Long id) {
        return R.success(tenantDatabasesConverter.entity2Vo(tenantDatabasesService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody TenantDatabasesDTO dto) {
        this.tenantDatabasesService.save(tenantDatabasesConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{dsId}")
    public R update(@RequestBody TenantDatabasesDTO dto, @PathVariable Long dsId) {
        this.tenantDatabasesService.updateById(tenantDatabasesConverter.dto2Entity(dto, dsId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.tenantDatabasesService.removeById(id);
        return R.success();
    }

}